#!/usr/bin/perl -w
use strict;
use Getopt::Long;
use lib "/net/cpp-group/Leo/bin";
use parse_fasta;
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<"USAGE";

USAGE:

    fa_to_pamlsequences.pl sequences.fa [--help]
		
		This script accepts sequences in the fasta format either in
		STDIN or on the command line and produces sequences reformatted
		for PAML on STDOUT

USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
GetOptions('help' => \$help);

die $usage if ($help);

#_________________________________________________________________________________________

#   save_sequences_callback
#                       $annotation     annotation line
#                       \@sequence      sequence lines
#                       $line count     line count in file
#                       $acc_pos        accession line file position
#                       $sequence_count index of current sequence
#                       $data           see below

#_________________________________________________________________________________________
sub save_sequences_callback($$$$$$)
{
    my ($acc,  $seq, $save_sequences) = @_[0, 1, 5];

	$save_sequences->[0]{$acc} = join "", @$seq;
}

my %sequences;

unshift(@ARGV, '-') unless @ARGV;
while (my $ARGV = shift @ARGV)
{
	unless (open (ARGV, $ARGV))
	{
		warn "Can't open $ARGV:$!\n";
		next;
	}
	my $cnt_seq =parse_fasta::parse_sequences(  *ARGV,
												\&save_sequences_callback,
												[\%sequences]);
	close ARGV;

}

my @accessions =  keys %sequences;
my $cnt_sequences = @accessions;
die "Error:\n\tNo sequences parsed.\n" unless ($cnt_sequences);

print "$cnt_sequences  ", length ($sequences{$accessions[0]}), "\n";
for (@accessions)
{
	/(\S+)/;
	print "$1\n$sequences{$_}\n";
}

